/**
 * 
 */
package com.angis.fx.activity.draft;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.angis.fx.activity.R;
import com.angis.fx.activity.jcdj.DailyCheckResultActivity;
import com.angis.fx.data.Draft;
import com.angis.fx.data.DraftInfo;
import com.angis.fx.db.CheckDraftDBHelper;

/**
 * @author Han
 * 
 */
public class TodayAndDraftActivity extends Activity {

	private ListView mListView;

	private List<Draft> mDraftList;

	private int mLastPosition = -1;

	private View mLastView;
	
	private Draft mCheckDraft;
	
	private int mPage = 0;

	private int mPageIndex = 1;

	private int mTotalCount;

	private Button mFirstPage;

	private Button mPreviousPage;

	private Button mNextPage;

	private Button mLastPage;
	private TextView pageShow;
	SimpleAdapter lAdapter ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.todayanddraft);

		mListView = (ListView) findViewById(R.id.todayanddraft);
		mDraftList = new ArrayList<Draft>();
//		mDraft = new Draft();
//		mDraft.setCsId("R050926103038199");
//		mDraft.setCsName("杭州花样年华文化有限公司");
//		mDraft.setCsType("娱乐场所");
//		mDraft.setInspectType("明查");
//		mDraft.setArrivalTime("2010.10.12 00:00:00");
//		mDraft.setArrivalTime("2010.10.12 00:00:00");
		
		mFirstPage = (Button) findViewById(R.id.firstpage);
		mPreviousPage = (Button) findViewById(R.id.previouspage);
		mNextPage = (Button) findViewById(R.id.nextpage);
		mLastPage = (Button) findViewById(R.id.lastpage);
		pageShow=(TextView) findViewById(R.id.pageshow);
		
		CheckDraftDBHelper helper=new CheckDraftDBHelper(this);
		//List<DraftInfo> list =helper.findDraftByPath(mPageIndex);
		mTotalCount=helper.totalCount();
		helper.closeDB();
		if(mTotalCount!=0){
			setPage(1);
		}
		displayResults();
		
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> pAdapterView, View pView,
					int pSelectedIndex, long arg3) {
				if (pSelectedIndex != mLastPosition) {
					setVisible(pView);
					setGone(mLastView);
					mLastPosition = pSelectedIndex;
					mLastView = pView;
					mCheckDraft=mDraftList.get(pSelectedIndex);
					Button lDetailsButton = (Button)pView.findViewById(R.id.inspectdetailsbutton);
					lDetailsButton.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							Intent lIntent = new Intent();
							lIntent.putExtra("draftId", mCheckDraft.getId());
							if("1".equals(mCheckDraft.getCslb())) {
								lIntent.setClass(TodayAndDraftActivity.this, DailyCheckResultActivity.class);
								TodayAndDraftActivity.this.startActivity(lIntent);
							}else if("2".equals(mCheckDraft.getCslb())) {
								lIntent.setClass(TodayAndDraftActivity.this, NoCedulaTodayCheckActivity.class);
								TodayAndDraftActivity.this.startActivity(lIntent);
							}
						}
					});
					
				} 
			}
		});
		
		setButtonStyle();
//		Button lDetailsButton = (Button) findViewById(R.id.cslicensewordvalue);
//		lDetailsButton.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				Intent lIntent = new Intent();
//				lIntent.putExtra("csid", lPI.getCsId());
//				lIntent.setClass(BriefSearchResultActivity.this, ChangsuoDetailsActivity.class);
//				BriefSearchResultActivity.this.startActivity(lIntent);
//				
//			}
//		});
		mFirstPage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				mPageIndex = 1;
				setPage(mPageIndex);
				displayResults();
				setButtonStyle();
			}
		});
		mPreviousPage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPageIndex = mPageIndex - 1;
				setPage(mPageIndex);
				displayResults();
				setButtonStyle();
			}
		});

		mNextPage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPageIndex = mPageIndex + 1;
				setPage(mPageIndex);
				displayResults();
				setButtonStyle();
			}
		});

		mLastPage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPageIndex = (mTotalCount%10 == 0)? mTotalCount/10:mTotalCount/10+1;
				setPage(mPageIndex);
				displayResults();
				setButtonStyle();
			}
		});
	}

	
	private void displayResults(){
		CheckDraftDBHelper helper=new CheckDraftDBHelper(this);
		List<DraftInfo> list =helper.findDraftByPath(mPageIndex,"0");
		helper.closeDB();
		mDraftList=new ArrayList<Draft>();
		for(DraftInfo info :list){
			Draft mDraft = new Draft();
			mDraft.setArrivalTime(info.getArrivaltime());
			mDraft.setCsId(info.getCsid());
			mDraft.setCsName(info.getContitle());
			mDraft.setCsType(info.getAreatype());
			mDraft.setCslb(info.getCslb());
			mDraft.setInspectType(info.getCheckxs());
			mDraft.setEndTime(info.getLeavetime());
			mDraft.setId(info.getId());
			mDraftList.add(mDraft);
		}
		lAdapter = new SimpleAdapter(this, mDraftList,
				R.layout.todayanddraftlayout, new String[] { "", "csname",
						"cstype", "inspecttype"}, new int[] { 0,
						R.id.changsuonamedisplay, R.id.changsuotypedisplay,
						R.id.inspecttypedisplay });
		mListView.setAdapter(lAdapter);
		//mListView.invalidateViews();
	}
	/*
	 */
	private void setVisible(View view) {
		if (view == null)
			return;
		LinearLayout layout = (LinearLayout) view;
//		layout.findViewById(R.id.changsuoaddressdisplay).setVisibility(
//				View.VISIBLE);
//		layout.findViewById(R.id.changsuotypedisplay).setVisibility(
//				View.VISIBLE);
		layout.findViewById(R.id.inspectdetailslayout).setVisibility(
				View.VISIBLE);
	}

	/*
	 */
	private void setGone(View view) {
		if (view == null)
			return;
		LinearLayout layout = (LinearLayout) view;
//		layout.findViewById(R.id.changsuoaddressdisplay).setVisibility(
//				View.GONE);
//		layout.findViewById(R.id.changsuotypedisplay).setVisibility(View.GONE);
		layout.findViewById(R.id.inspectdetailslayout).setVisibility(View.GONE);
	}

	
	private void setButtonStyle() {
		if (mTotalCount == 0) {
			//pageShow.setText("1/1");
			setPageControlButtonGone();
			Toast.makeText(this, "没有符合该条件的记录", Toast.LENGTH_LONG).show();
		} else {
			
			setPageControlButtonVisible();
			if (mPageIndex == 1) {
				mFirstPage.setEnabled(false);
				mPreviousPage.setEnabled(false);
				if (mTotalCount <= 10) {
					mNextPage.setEnabled(false);
					mLastPage.setEnabled(false);
				}else{
					mNextPage.setEnabled(true);
					mLastPage.setEnabled(true);
				}
			} else {
				mFirstPage.setEnabled(true);
				mPreviousPage.setEnabled(true);
				if (mTotalCount % 10 == 0) {
					if (mPageIndex < mTotalCount / 10) {
						mNextPage.setEnabled(true);
						mLastPage.setEnabled(true);
					} else {
						mNextPage.setEnabled(false);
						mLastPage.setEnabled(false);
					}
				} else {
					if (mPageIndex < mTotalCount / 10 + 1) {
						mNextPage.setEnabled(true);
						mLastPage.setEnabled(true);
					} else {
						mNextPage.setEnabled(false);
						mLastPage.setEnabled(false);
					}
				}
			}

		}
	}
	private void setPageControlButtonGone() {
		mFirstPage.setVisibility(View.GONE);
		mPreviousPage.setVisibility(View.GONE);
		mNextPage.setVisibility(View.GONE);
		mLastPage.setVisibility(View.GONE);
	}

	private void setPageControlButtonVisible() {
		mFirstPage.setVisibility(View.VISIBLE);
		mPreviousPage.setVisibility(View.VISIBLE);
		mNextPage.setVisibility(View.VISIBLE);
		mLastPage.setVisibility(View.VISIBLE);
	}
	
	private void setPage(int page){
		int total = (mTotalCount%10 == 0)? mTotalCount/10:mTotalCount/10+1;
		pageShow.setText(page+"/"+total);
	}
}
